import modelExtend from 'dva-model-extend'
import { query, detail, approve, deny } from './services/transferToBalanceRecordService'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router';

export default modelExtend(lwjPageModel, {
  namespace: "transferToBalanceRecord",
  state: {
    currentItem: {},
    selectRows: [],
    selectedRowKeys: [],
    pageOptions: {
      sort: 'createTime',
      order: 'desc'
    },
    isExpand: false,
    visible: false,
    detailVisible: false,
    auditVisible: false,
    queryParam: {},
    currentType: "SALES",
    distributorList: [{}],
    currentSelect: [{}],
    optionType: '',
    currentDistributor: {},
    usedAmount: '',
    auditType: '',
    fileVisible: true,

  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === "/wx/transferToBalanceRecord/transferToBalanceRecordList") {
          dispatch({ type: 'query', payload: {} })
        }
      })
    }

  },
  effects: {
    * approve({
      payload
    }, { select, call, put }) {
      yield call(approve, payload);
      yield put(routerRedux.push('/success', { listUrl: 'transferToBalanceRecord/transferToBalanceRecordList', detailUrl: 'transferToBalanceRecord/transferToBalanceRecordList' }));
    },
    * deny({
      payload
    }, { select, call, put }) {
      yield call(deny, payload);
      yield put(routerRedux.push('/success', { listUrl: 'transferToBalanceRecord/transferToBalanceRecordList', detailUrl: 'transferToBalanceRecord/transferToBalanceRecordList' }));
    },
    * detail({
      payload
    }, { select, call, put }) {
      const data = yield call(detail, payload);
      yield put({
        type: 'setCurrentItem',
        payload: data
      })
    },
    * query({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });
      const pageOptions = yield select(({ transferToBalanceRecord }) => transferToBalanceRecord.pageOptions)
      const queryParam = yield select(({ transferToBalanceRecord }) => transferToBalanceRecord.queryParam);
      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam,

        }
      };
      const data = yield call(query, queryPayload);
      yield put({
        type: 'freshState',
        payload: {

        }
      })
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    }
  },
  reducers: {
    onSelectChange(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    setCurrentTab(state, { payload }) {
      return {
        ...state,
        currentType: payload
      }
    },
    freshState(state, { payload }) {
      return {
        ...state,
        selectRows: [],
        selectedRowKeys: []
      }
    },
    setCurrentItem(state, { payload }) {
      return {
        ...state,
        currentItem: payload
      }
    },
    setOptionType(state, { payload }) {
      return {
        ...state,
        optionType: payload
      }
    },
    setDetailVisible(state, { payload }) {
      return {
        ...state,
        detailVisible: payload
      }
    },
    closeAllModal(state, { payload }) {
      return {
        ...state,
        auditVisible: false,
        detailVisible: false
      }
    },
    setVisible(state, { payload }) {
      return {
        ...state,
        visible: payload
      }
    },
    setSelectRecord(state, { payload }) {
      return {
        ...state,
        currentSelect: payload
      }
    },
    setAuditVisible(state, { payload }) {
      return {
        ...state,
        auditVisible: payload.auditVisible,
        auditType: payload.auditType
      }
    },
    setAuditType(state, { payload }) {
      return {
        ...state,
        auditType: payload
      }
    },
    setFileVisible(state, { payload }) {
      return {
        ...state,
        fileVisible: payload
      }
    },
  }
})
